/******************************************
Server.java
Universidad del Valle de Guatemala
Algoritmos y Estructura de Datos
Autor:
 * Martín Guzmán
 * Karen Tojín
 * Jeffry Turcios
 * David Coronado
Fecha: 28 de noviembre de 2009
Descripción:
*******************************************/

import  org.jdom.JDOMException;
import  org.jdom.input.SAXBuilder;
import  java.io.IOException;
import  org.jdom.*;
import  java.util.*;

public class XML_Parser {


    public static Request parser(Document doctemp){

        SAXBuilder builder = new SAXBuilder();
        Document doc = doctemp;
        Element root = doc.getRootElement();
        List<Element> children = root.getChildren();
        Hashtable<String,Integer> options_table = new Hashtable();
        Hashtable<String,Integer> fields_table = new Hashtable();
        LinkedList<String> fields = new LinkedList<String>();
        int tipo = 0;

        options_table.put("ingresar", 1);
        options_table.put("modificar", 2);
        options_table.put("borrar", 3);
        options_table.put("buscar", 4);

        fields_table.put("carnet", 0);
        fields_table.put("nombre", 1);
        fields_table.put("apellido", 2);
        fields_table.put("carrera", 3);


        if(options_table.containsKey(root.getName()))
            tipo = options_table.get(root.getName());

        for(int i = 0; i < 4; i++)
            fields.add(null);
        //System.out.println(children.size());
        for(int i = 0; i < children.size(); i++)
            fields.set(fields_table.get(children.get(i).getName()), children.get(i).getText());

        return(new Request(tipo, fields));
    }
}

